//
//  RNOLBasicInfoViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLBasicInfoViewController.h"

@interface RNOLBasicInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSDictionary *dataDict;

@end

@implementation RNOLBasicInfoViewController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
//        _tableView.height -= kBottomHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 20)];
        header.backgroundColor = kBgColor;
        _tableView.tableHeaderView = header;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"基本信息"];
    [RNOLLoginTool setNavgationBarNeedAnimation:YES];
    [self setup];
}

- (void)setup{
    self.dataDict = @{
                      @"0" : @[@"头像"],
                      @"1" : @[@"手机号码",@"我的二维码",@"物流地址管理"]
                      };
    [self.tableView reloadData];
    
    UIButton *loginOutBtn = [RNOLUnitTool createRNOLStyleBlueButtonWithButtonTitle:@"退出登录"];
    loginOutBtn.height = 50;
    loginOutBtn.y = KHIGHT -kNavBarHeight - 50;
    loginOutBtn.y -= kBottomHeight;
    [loginOutBtn addTarget:self action:@selector(loginOut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: loginOutBtn];
    
}

#pragma mark- 退出登录
- (void)loginOut:(UIButton *)button{
    
    [self showSuccessMessageWithText:@"退出登录成功!"];
}

#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataDict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.dataDict[@(section).stringValue];
    return items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0) {
        UIImageView *headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH - 100, 20, 60 , 60)];
        headerImgView.backgroundColor = KRedColor;
        [headerImgView setAllCorner: headerImgView.width/2.0];
        [cell.contentView addSubview: headerImgView];
    }
    
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    cell.textLabel.text = items[indexPath.row];
    cell.textLabel.textColor = kTextColor;
    cell.textLabel.font = KFontSize(15);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [RNOLUnitTool createViewWithBackgroundColor:kBgColor];
}

- (void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    NSString *title = items[indexPath.row];
    if (kCompareTwoString(title,@"头像")) {
        [self changeUserHeaderImage];
    }else if (kCompareTwoString(title, @"手机号码")){
        
    }else if (kCompareTwoString(title, @"我的二维码")){
        
    }else if (kCompareTwoString(title, @"物流地址管理")){
        
    }
    
}

#pragma mark - 更换用户头像
- (void)changeUserHeaderImage{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    //相册参数配置，configuration有默认值，可直接使用并对其属性进行修改
    actionSheet.configuration.maxSelectCount = 1; //限制最多选择一张
    actionSheet.configuration.maxPreviewCount = 10; //预览数量
    actionSheet.configuration.allowEditImage = YES;
    actionSheet.configuration.navBarColor = kAppMainColor;
    actionSheet.configuration.saveNewImageAfterEdit = NO;
    //编辑选择后的图
    actionSheet.configuration.editAfterSelectThumbnailImage = YES;
    actionSheet.configuration.clipRatios = @[GetClipRatio(1, 1)];//设置裁剪比例1:1
    //如调用的方法无sender参数，则该参数必传
    actionSheet.sender = self;
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nullable images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        NSLog(@"%@",images.firstObject);
//        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//        [SVProgressHUD showWithStatus:@"图片上传中..."];
//        [self.addBabyView.headImageButton setBackgroundImage: images.firstObject forState:UIControlStateNormal];;
//        [JJFileUploadManager upLoadFileWithImage: images.firstObject callBack:^(NSString *imagePath) {
//            self.headImage = imagePath ;
//            [SVProgressHUD dismiss];
//        }];
    }];
    [actionSheet showPreviewAnimated: YES];
    
}




@end
